import { getTrailListApi } from '@/api/custcare/visitor/visitor'
import { VisitorParam } from '@/api/custcare/visitor/VisitorModel'
import { StatusCode } from '@/http/request'
import { EditType } from '@/types/BaseEnum'
import { formatDate } from '@/utils/filters'
import { nextTick, onMounted, reactive, ref } from 'vue'

export default function useTrail(visitorInfo: any) {
    const mainLayoutHeight = ref(0)

    const trailList = reactive({
        data: []
    })

    const recordRef = ref<{
        show: (type: string, channelId: string, visitorInfo: any) => void
    }>()

    // 消息记录
    const showRecordBtn = async (custcare: any) => {
        recordRef.value?.show(EditType.EDIT, custcare, visitorInfo)
    }

    const getTrailList = async () => {
        const visitor_id = visitorInfo.id
        let res = await getTrailListApi(
            reactive<VisitorParam>({
                id: visitor_id
            })
        )
        if (res && res.code == StatusCode.Success) {
            res.data.forEach((item: any) => {
                item.created_at = formatDate(item.created_at)
            })
            trailList.data = res.data
        }
    }

    onMounted(() => {
        getTrailList()
        nextTick(() => {
            mainLayoutHeight.value = window.innerHeight - 230
        })
    })

    return {
        mainLayoutHeight,
        trailList,
        recordRef,
        showRecordBtn
    }
}
